using System;
using System.Data;
using System.Xml;
using System.Runtime.Serialization;
using System.Collections;
using Widgetsphere.Core.Exceptions;
using Widgetsphere.Core.DataAccess;
using ACME.Northwind.Business;
using ACME.Northwind.Business.Objects;
using ACME.Northwind.Business.Rules;
using ACME.Northwind.Business.SelectCommands;
using System.Collections.Generic;

namespace ACME.Northwind.Domain.Objects
{
	/// <summary>
	/// This is an customizable extender for the domain class associated with the 'EmployeeTerritory' object
	/// </summary>
	[Serializable()]
	partial class DomainEmployeeTerritory : PersistableDomainObjectBase
	{
		#region Member Variables

		private const string TABLE_NAME = "EmployeeTerritoryCollection";
		internal DomainEmployeeTerritoryCollection ParentCol;

		#endregion

		#region Constructor

		internal DomainEmployeeTerritory(DataRowBuilder rb) : base(rb) 
		{
			this.ParentCol = ((DomainEmployeeTerritoryCollection)(base.Table));
		}

		#endregion

		#region Properties

		internal EmployeeTerritoryPrimaryKey PrimaryKey
		{
			get { return new EmployeeTerritoryPrimaryKey(this.EmployeeId, this.TerritoryId); }
		}

		internal int EmployeeId
		{
			get
			{
				try
				{
					return ((int)(base[this.ParentCol.EmployeeIdColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
		}

		internal string TerritoryId
		{
			get
			{
				try
				{
					if (base[this.ParentCol.TerritoryIdColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.TerritoryIdColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
		}

		internal virtual DomainEmployee EmployeeItem
		{
			get { return (DomainEmployee)base.GetParentRow(this.ParentCol.EmployeeEmployeeTerritoryRelation); }
			set
			{
				if (value != null)
				{
					DomainEmployeeCollection list = null;
					if (value.GetType() == typeof(DomainEmployee))
						list = (DomainEmployeeCollection)this.ParentCol.SubDomain.GetDomainCollection(Collections.EmployeeCollection);
					bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
					this.ParentCol.SubDomain.EnforceConstraints = false;
					DomainEmployee employee = (DomainEmployee)value;
					if(employee.ParentCol != list)
					{
						this.ParentCol.SubDomain.Merge(new DataRow[] { employee }, false, MissingSchemaAction.Error);
						employee = list.GetEmployee(employee.EmployeeId);
					}
					base.SetParentRow(employee, this.ParentCol.EmployeeEmployeeTerritoryRelation);
					this.ParentCol.SubDomain.EnsureParentRowsExist();				
					this.ParentCol.SubDomain.EnforceConstraints = oldEc;
				}
				else
				{
					if(this.ParentCol.EmployeeIdColumn.AllowDBNull)
					{
						base.SetNull(this.ParentCol.EmployeeIdColumn);
					}
					else
					{
						this.Delete();
					}
				}
			}
		}

		internal virtual DomainTerritory TerritoryItem
		{
			get { return (DomainTerritory)base.GetParentRow(this.ParentCol.TerritoryEmployeeTerritoryRelation); }
			set
			{
				if (value != null)
				{
					DomainTerritoryCollection list = null;
					if (value.GetType() == typeof(DomainTerritory))
						list = (DomainTerritoryCollection)this.ParentCol.SubDomain.GetDomainCollection(Collections.TerritoryCollection);
					bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
					this.ParentCol.SubDomain.EnforceConstraints = false;
					DomainTerritory territory = (DomainTerritory)value;
					if(territory.ParentCol != list)
					{
						this.ParentCol.SubDomain.Merge(new DataRow[] { territory }, false, MissingSchemaAction.Error);
						territory = list.GetTerritory(territory.TerritoryId);
					}
					base.SetParentRow(territory, this.ParentCol.TerritoryEmployeeTerritoryRelation);
					this.ParentCol.SubDomain.EnsureParentRowsExist();				
					this.ParentCol.SubDomain.EnforceConstraints = oldEc;
				}
				else
				{
					if(this.ParentCol.TerritoryIdColumn.AllowDBNull)
					{
						base.SetNull(this.ParentCol.TerritoryIdColumn);
					}
					else
					{
						this.Delete();
					}
				}
			}
		}

		#endregion

		#region Collection Operation Methods

		/// <summary>
		/// Serves as a hash function for this particular type.
		/// </summary>
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public override bool Equals(object obj)
		{
			if (obj == null) return false;
			if (obj.GetType() != this.GetType()) return false;

			if ((this.RowState == DataRowState.Deleted) ||
				(((DataRow)obj).RowState == DataRowState.Deleted) ||
				(this.RowState == DataRowState.Detached) ||
				(((DataRow)obj).RowState == DataRowState.Detached))
				return false;

			return ((DomainEmployeeTerritory)obj).PrimaryKey.Equals(this.PrimaryKey);
		}

		public override void Persist()
		{
			DomainHelper.PersistDomainObject(this, this.ParentCol.SubDomain, Collections.EmployeeTerritoryCollection, this.ParentCol.Modifier);
		}

		public override void Remove()
		{
			this.ParentCol.RemoveEmployeeTerritory(this);
		}

		#endregion

		#region Relationship Methods
		internal virtual void ReleaseNonIdentifyingRelationships()
		{
		}

		#endregion
	}
}
